package serveur;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Serveur version1 : Renvoie un message recu par un client puis ferme la
 * connexion. Les autres client sont mis en attente pendant l'echange.
 * 
 * @author Erik & Lucas
 */
public class Serveur1 {

	private static ServerSocket serveurSocket;

	public static void main(String[] args) {
		try {
			serveurSocket = new ServerSocket(2009);
			System.out.println("Serveur lance sur le port: 2009, en attente de connexion d'un client");

			while (true) {
				Socket socket = serveurSocket.accept();

				System.out.println("Connexion TCP etablie");

				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

				PrintWriter out;

				out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

				String str = "";
				str = in.readLine();
				System.out.println("client@" + socket.getInetAddress() + " > " + str);
				out.println(str);
				in.close();
				out.close();
				socket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
